<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/blog/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/blog/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="nginx反向代理到tomcat在Nginx中，一个server {}块往往用来配置一个比较大的项目，一般就是对于一个域名的所有配置，一个server块内一般有多个location来定义多个请求规则，比如域名和根目录配置，静态资源支持，php fastcgi请求，url重写，错误页面配置等多种配置，所以nginx代理tomcat和前面的转发php-fpm一样，就是将请求转发到后端的动态模块去处理该">
<meta name="keywords" content="nginx,tomcat">
<meta property="og:type" content="article">
<meta property="og:title" content="nginx反向代理到tomcat">
<meta property="og:url" content="https://gitee.com/jie10406/blog.git/2019/09/10/nginx反向代理到tomcat/index.html">
<meta property="og:site_name" content="渐行渐远的个人博客">
<meta property="og:description" content="nginx反向代理到tomcat在Nginx中，一个server {}块往往用来配置一个比较大的项目，一般就是对于一个域名的所有配置，一个server块内一般有多个location来定义多个请求规则，比如域名和根目录配置，静态资源支持，php fastcgi请求，url重写，错误页面配置等多种配置，所以nginx代理tomcat和前面的转发php-fpm一样，就是将请求转发到后端的动态模块去处理该">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-09-10T08:20:14.256Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="nginx反向代理到tomcat">
<meta name="twitter:description" content="nginx反向代理到tomcat在Nginx中，一个server {}块往往用来配置一个比较大的项目，一般就是对于一个域名的所有配置，一个server块内一般有多个location来定义多个请求规则，比如域名和根目录配置，静态资源支持，php fastcgi请求，url重写，错误页面配置等多种配置，所以nginx代理tomcat和前面的转发php-fpm一样，就是将请求转发到后端的动态模块去处理该">
  <link rel="alternate" href="/blog/atom.xml" title="渐行渐远的个人博客" type="application/atom+xml">
  <link rel="canonical" href="https://gitee.com/jie10406/blog.git/2019/09/10/nginx反向代理到tomcat/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>nginx反向代理到tomcat | 渐行渐远的个人博客</title>
  <meta name="generator" content="Hexo 3.9.0">
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">渐行渐远的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/blog/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-about">
      
    

    <a href="/blog/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/blog/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/blog/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/blog/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/jie10406/blog.git/blog/2019/09/10/nginx反向代理到tomcat/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="渐行渐远">
      <meta itemprop="description" content="嘿嘿">
      <meta itemprop="image" content="/blog/images/jxjy.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="渐行渐远的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">nginx反向代理到tomcat

          
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-09-10 15:52:25 / 修改时间：16:20:14" itemprop="dateCreated datePublished" datetime="2019-09-10T15:52:25+08:00">2019-09-10</time>
            </span>
          
            

            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/blog/categories/nginx/" itemprop="url" rel="index"><span itemprop="name">nginx</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/blog/categories/nginx/tomcat/" itemprop="url" rel="index"><span itemprop="name">tomcat</span></a></span>

                
                
              
            </span>
          

          
            <span class="post-meta-item" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="nginx反向代理到tomcat"><a href="#nginx反向代理到tomcat" class="headerlink" title="nginx反向代理到tomcat"></a>nginx反向代理到tomcat</h4><p>在Nginx中，一个server {}块往往用来配置一个比较大的项目，一般就是对于一个域名的所有配置，一个server块内一般有多个location来定义多个请求规则，比如域名和根目录配置，静态资源支持，php fastcgi请求，url重写，错误页面配置等多种配置，所以nginx代理tomcat和前面的转发php-fpm一样，就是将请求转发到后端的动态模块去处理该请求</p>
<p>　　下面在server中简单配置一个location局部规则，将指定的nginx请求转发至tomcat容器</p>
<p>　　假设tomcat已经正常运行，端口号为8080</p>
<p>然后在server {}块最后，补充下面的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">location ^~ /tomcat/ &#123;</span><br><span class="line">          proxy_pass   http://127.0.0.1:8080/;</span><br><span class="line">          proxy_redirect  off;</span><br><span class="line">          proxy_set_header  X-Real-IP $remote_addr;</span><br><span class="line">          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<p>所以这里使用/tomcat/请求来确定是否转发到tomcat容器，前面的^~就是匹配任意的域名或者IP地址部分，所以当访问<a href="http://ip/tomcat这样的请求时，那么tomcat容器就会起作用，和访问http://ip:8080效果是一样的，包括容器下的项目都是完全一样的" target="_blank" rel="noopener">http://ip/tomcat这样的请求时，那么tomcat容器就会起作用，和访问http://ip:8080效果是一样的，包括容器下的项目都是完全一样的</a></p>
<p>Nginx做反向代理，如果在header设置了Host参数，同时如果有协议和二级目录有不一致的情况的时候，<br>当后端服务做302、301跳转的时候,需要用proxy_redirect将后端设置在response header中的Location做转换.</p>
<p>proxy_set_header用来重定义发往后端服务器的请求头</p>
<p><strong>Value</strong>值可以是包含文本、变量或者它们的组合。常见的设置如：<br>proxy_set_header Host $proxy_host;<br>proxy_set_header X-Real-IP $remote_addr;<br>proxy_set_header X-Forwarded-For $remote_addr;</p>
<p>注意：在nginx的配置文件中，如果当前模块中没有proxy_set_header的设置，则会从上级别继承配置。继承顺序为：http, server, location。</p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/blog/tags/nginx/" rel="tag"># nginx</a>
            
              <a href="/blog/tags/tomcat/" rel="tag"># tomcat</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/blog/2019/08/27/vue部署到nginx/" rel="next" title="vue打包部署到nginx">
                  <i class="fa fa-chevron-left"></i> vue打包部署到nginx
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc" data-target="post-toc-wrap">
          文章目录
        </li>
        <li class="sidebar-nav-overview" data-target="site-overview-wrap">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#nginx反向代理到tomcat"><span class="nav-number">1.</span> <span class="nav-text">nginx反向代理到tomcat</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/blog/images/jxjy.jpg"
      alt="渐行渐远">
  <p class="site-author-name" itemprop="name">渐行渐远</p>
  <div class="site-description" itemprop="description">嘿嘿</div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">2</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/blog/categories/">
          
        
        
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/blog/tags/">
          
        
        
        
          
        
          
        
          
        
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
  <div class="feed-link motion-element">
    <a href="/blog/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://www.alloyteam.com/nav/" title="http://www.alloyteam.com/nav/" rel="noopener" target="_blank">Web前端导航</a>
        </li>
      
        <li class="links-of-blogroll-item">
          <a href="http://www.chuangzaoshi.com/code" title="http://www.chuangzaoshi.com/code" rel="noopener" target="_blank">创造狮导航</a>
        </li>
      
        <li class="links-of-blogroll-item">
          <a href="http://e.xitu.io/" title="http://e.xitu.io/" rel="noopener" target="_blank">掘金酱</a>
        </li>
      
        <li class="links-of-blogroll-item">
          <a href="https://www.v2ex.com/" title="https://www.v2ex.com/" rel="noopener" target="_blank">V2EX</a>
        </li>
      
        <li class="links-of-blogroll-item">
          <a href="https://www.v2ex.com/" title="https://www.v2ex.com/" rel="noopener" target="_blank">印记中文</a>
        </li>
      
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">渐行渐远</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.3.0</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="总访客量">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  
    <span class="post-meta-divider">|</span>
  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="总访问量">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>












        
      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/blog/js/utils.js?v=7.3.0"></script><script src="/blog/js/motion.js?v=7.3.0"></script>
<script src="/blog/js/schemes/pisces.js?v=7.3.0"></script>

<script src="/blog/js/next-boot.js?v=7.3.0"></script>



  








  <script src="/blog/js/local-search.js?v=7.3.0"></script>














  

  

  

</body>
</html>
